using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;
namespace LdExpress
{
    /// <summary>
    /// QuanHuyen
    /// </summary>
    public class Huyen
    {
        public long ID = 0;
        public string Ma = "";
        public string Ten = "";
        public float Kms = 0;
        public long TinhID = 0;
        public Huyen()
        {
        }
        public Huyen(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = DBModule.GetNewID(typeof(Huyen), "tbl_Huyen", cn, trans);
                // build SQL statement
                strSQL = "Insert into tbl_Huyen (" +
                             "Ma," +
                             "Ten," +
                             "Kms," +
                             "TinhID) " +
                         " values(" +
                             "'" + DBModule.RefineString(Ma) + "'," +
                             "'" + DBModule.RefineString(Ten) + "'," +
                             Kms.ToString() + "," +
                             TinhID.ToString() + ")";
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Huyen", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update tbl_Huyen set " +
                             "Ma='" + DBModule.RefineString(Ma) + "'," +
                             "Ten='" + DBModule.RefineString(Ten) + "'," +
                              "Kms=" + Kms.ToString() + "," +
                             "TinhID=" + TinhID.ToString() + "" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Huyen", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            string strSQL = "DELETE FROM tbl_Huyen WHERE ID = " + this.ID.ToString();
            //Run SQL Statement
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Huyen", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_Huyen WHERE ID = " + ID.ToString();
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Ma")) Ma = dr["Ma"].ToString();
                if (!dr.IsNull("Ten")) Ten = dr["Ten"].ToString();
                if (!dr.IsNull("TinhID")) TinhID = long.Parse(dr["TinhID"].ToString());
                if (!dr.IsNull("Kms")) Kms = float.Parse(dr["Kms"].ToString());
            }
        }
        public void Load(string strMa, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_Huyen WHERE Trim(Ma)='" + DBModule.RefineString(strMa) + "'";
            GV.Response.Write(strSQL);
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Ma")) Ma = dr["Ma"].ToString();
                if (!dr.IsNull("Ten")) Ten = dr["Ten"].ToString();
                if (!dr.IsNull("TinhID")) TinhID = long.Parse(dr["TinhID"].ToString());
                if (!dr.IsNull("Kms")) Kms = float.Parse(dr["Kms"].ToString());
            }
        }
        public void LoadByName(string strName, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_Huyen WHERE Trim(Ten)='" + DBModule.RefineString(strName) + "'";
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Ma")) Ma = dr["Ma"].ToString();
                if (!dr.IsNull("Ten")) Ten = dr["Ten"].ToString();
                if (!dr.IsNull("TinhID")) TinhID = long.Parse(dr["TinhID"].ToString());
                if (!dr.IsNull("Kms")) Kms = float.Parse(dr["Kms"].ToString());
            }
        }
        public void GetList(string strCondition, string TinhID, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_Huyen where 1=1 ";

            if ((TinhID != "") && (TinhID != null))
            {
                try
                {
                    int tempTinhID = int.Parse(TinhID);
                    strSQL += " and TinhID=" + tempTinhID.ToString();
                }
                catch { }
            }
            if ((strCondition != null) && (strCondition != ""))
            {
                if (strCondition != "All") strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            else
                strSQL = strSQL + " Order By TenTinh, TenHuyen";
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
        public void GetList(string strCondition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_Huyen where 1=1 ";
            if ((strCondition != null) && (strCondition != ""))
            {
                if (strCondition != "All") strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public static void doSearch(string strKeywords, string TinhID, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select [ID] from tbl_Huyen where 1=1 ";
            if ((strKeywords != "") && (strKeywords != null))
            {
                strSQL += " AND ( Ma like N'%" + strKeywords + "%' or  Ten like N'%" + strKeywords + "%')";
            }

            if ((TinhID != "") && (TinhID != null))
            {
                try
                {
                    int tempTinhID = int.Parse(TinhID);
                    strSQL += " and TinhID=" + tempTinhID.ToString();
                }
                catch { }
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }

        public static string GetNameFromID(long tinhID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT top 1 Ten FROM tbl_Huyen WHERE ID = " + tinhID.ToString();
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("Ten")) return dr["Ten"].ToString();
            }
            return "";
        }
        public static float Distance(long DistrictID, long LocationID)
        {
            float retVal = 0;
            Huyen objH = new Huyen();
            objH.ID = DistrictID;
            objH.Load(GV.cn, GV.trans);
            Tinh objT = new Tinh();
            objT.ID = objH.TinhID;
            objT.Load(GV.cn, GV.trans);
            if (objT.VungID == LocationID)
                retVal = objH.Kms;
            return retVal;
        }
    }
}
